package com.shigu.account.service;

import com.opentae.data.mall.beans.CodeMap;
import com.opentae.data.mall.beans.UserInviteInfo;
import com.opentae.data.mall.interfaces.CodeMapMapper;
import com.opentae.data.mall.interfaces.UserInviteInfoMapper;
import com.shigu.MallResponse;
import com.shigu.account.api.VipManagerService;
import com.shigu.exceptions.AdminException;
import com.shigu.exceptions.WapException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/*
 * create by  过儿  2019-11-18
 * 邀请码服务
 */
@Service
public class InviteCodeService {

    @Autowired
    UserInviteInfoMapper userInviteInfoMapper;

    @Autowired
    CodeMapMapper codeMapMapper;

    @Resource(name="vipManagerServiceImpl")
    VipManagerService vipManagerService;

    /**
     * @description 添加定制
     * @author 郭尔
     * @date 2019-11-18 13:44
     */
    public MallResponse addCode(Long mallUserId, String inviteCode) throws AdminException {
        if(codeMapMapper.countCode(inviteCode) > 0){
            throw new AdminException("该邀请码已被使用");
        }
        CodeMap code = new CodeMap();
        code.setMallUserId(mallUserId);
        code.setInviteCode(inviteCode);
        codeMapMapper.insertSelective(code);

        UserInviteInfo info = new UserInviteInfo();
        info.setMallUserId(mallUserId);
        info.setInviteCode(inviteCode);
        userInviteInfoMapper.updateByPrimaryKeySelective(info);

        vipManagerService.loadCodeMap();

        return new MallResponse().success();
    }
}
